package com.example.user_center.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.user_center.model.domain.User;
import com.example.user_center.model.request.UserLoginRequest;
import com.example.user_center.model.request.UserRegisterRequest;
import com.example.user_center.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.example.user_center.constant.UserConstant.ADMIN_ROLE;
import static com.example.user_center.constant.UserConstant.USER_LOGIN_STATE;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/user")
public class UserController {

//    @Resource
    private UserService userService;

    @PostMapping("/register")
    public Long userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if(userRegisterRequest == null){
            return null;
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){
            return null;
        }
        return userService.userRegister(userAccount, userPassword, checkPassword);
    }

    @PostMapping("/login")
    public User userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest httpServletRequest){
        if(userLoginRequest == null){
            return null;
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount,userPassword)){
            return null;
        }

        return userService.userLogin(userAccount, userPassword,httpServletRequest);
    }

    @GetMapping("/search")
    public List<User> searchUsers(String username, HttpServletRequest request){
        //仅管理员可查询
        if(isAdmin(request))
        {
            return null;
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isAnyBlank(username)){
            queryWrapper.like("userName",username);
        }
        List<User> list = userService.list(queryWrapper);
        return list.stream().map(user -> userService.getSafetyUser(user)).collect(Collectors.toList());
    }


    @PostMapping("/delete")
    public boolean deleteUser(@RequestBody long id,HttpServletRequest request){
        if(isAdmin(request))
        {
            return false;
        }
        if(id<=0){
            return false;
        }
        return userService.removeById(id);
    }

// 判断用户是否为管理员
    private boolean isAdmin(HttpServletRequest request){
        // 获取当前会话中的用户对象
        Object userObject = request.getSession().getAttribute(USER_LOGIN_STATE);
        // 将用户对象转换为User类型
        User user = (User)userObject;
        // 判断用户对象是否为空，并且用户角色是否为管理员
        return user!=null&&user.getUserRole()==ADMIN_ROLE;
    }
}
